/**
 * 
 */
package server.alarmThreads;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;

import org.apache.log4j.Logger;

import server.config.ServerConfig;
import server.container.LocalAlarm;

/**
 * @author Christian
 * 
 */
public class LocalAlarmierer {

	/**
	 * 
	 */
	private static Logger logger = Logger.getLogger(LocalAlarmierer.class);
	private Clip clip;

	public void alarmiere(LocalAlarm alarm) {

		if (alarm.getAlarm().getSchleife().equals("06-52") || alarm.getAlarm().getFeuerwehrID() == 1) {

			logger.debug("starte local alarm");
			LocalAlarmierer player = new LocalAlarmierer();
			player.setMuteState(false);
			player.setVolume(65535);
			player.playWAV("sound/feuer.wav");
			logger.debug("Sound läuft!");
			try {
				Thread.sleep(30 * 1000);
			} catch (InterruptedException e) {
				logger.error("", e);
			}
			player.setVolume(5000);
			
			if(clip != null) {
				clip.close();
			}
		}
	}

	/**
	 * @param string
	 */
	private void play(String filePfad) {

		StringBuilder exceBuilder = new StringBuilder();
		exceBuilder.append(ServerConfig.getInstance().getVlcPfad());
		exceBuilder.append(" --qt-autosave-volume ");
		exceBuilder.append(filePfad);
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec(exceBuilder.toString());
		} catch (IOException e) {
			logger.error("", e);
		}
	}

	private void playWAV(String file) {

		File soundFile = new File(file);
		AudioInputStream sound;
		try {
			sound = AudioSystem.getAudioInputStream(soundFile);

			// load the sound into memory (a Clip)
			DataLine.Info info = new DataLine.Info(Clip.class,
					sound.getFormat());
			clip = (Clip) AudioSystem.getLine(info);
			clip.open(sound);

			// due to bug in Java Sound, explicitly exit the VM when
			// the sound has stopped.
			// clip.addLineListener(new LineListener() {
			// public void update(LineEvent event) {
			// if (event.getType() == LineEvent.Type.STOP) {
			// event.getLine().close();
			// System.exit(0);
			// }
			// }
			// });

			// play the sound clip
			clip.start();
			
		} catch (Exception e) {

			logger.error("", e);
		}
	}

	/**
	 * 
	 * @param wert
	 *            0-65535
	 */
	private void setVolume(int wert) {

		StringBuilder exceBuilder = new StringBuilder();
		exceBuilder.append(ServerConfig.getInstance().getNircmdPfad());
		exceBuilder.append(" ");
		exceBuilder.append("setsysvolume");
		exceBuilder.append(" ");
		exceBuilder.append(wert);
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec(exceBuilder.toString());
		} catch (IOException e) {
			logger.error("", e);
		}
		logger.info(exceBuilder.toString());
	}

	private void setMuteState(boolean mute) {

		StringBuilder exceBuilder = new StringBuilder();
		exceBuilder.append(ServerConfig.getInstance().getNircmdPfad());
		exceBuilder.append(" ");
		exceBuilder.append("mutesysvolume");
		exceBuilder.append(" ");
		if (mute) {
			exceBuilder.append(1);
		} else {
			exceBuilder.append(0);
		}
		Runtime runtime = Runtime.getRuntime();
		try {
			runtime.exec(exceBuilder.toString());
		} catch (IOException e) {
			logger.error("", e);
		}
	}

	public static void main(String[] args) {

		try {
			new ServerConfig("serverConfigLocal.cfg");
		} catch (IOException e) {
			logger.error("", e);
		}
		LocalAlarmierer player = new LocalAlarmierer();
		player.setMuteState(false);
		player.setVolume(65535);
		player.play("sound/feuer.mp3");
		try {
			Thread.sleep(10 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			logger.error("", e);
		}
		player.play("sound/feuer.mp3");
		try {
			Thread.sleep(10 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			logger.error("", e);
		}
		player.setVolume(26000);
	}

}
